package tom.vertx.core.demo15;

import com.google.common.collect.Lists;
import io.vertx.core.cli.CLI;
import io.vertx.core.cli.CommandLine;
import io.vertx.core.cli.annotations.*;
import io.vertx.core.impl.launcher.commands.StartCommand;
import lombok.ToString;

import java.io.File;

/**
 * 除了可以编程实现，还可以通过注解实现
 *
 * @author ZHUFEIFEI
 */
public class CliAnnotationDemo {

  public static void main(String[] args) {
    CLI cli = CLI.create(CopyCli.class);

    StringBuilder builder = new StringBuilder(500);

    CommandLine cmd = cli.parse(Lists.newArrayList("-h"));
    if (!cmd.isValid() && cmd.isAskingForHelp()) {
      cli.usage(builder);
      System.out.println(builder.toString());
    }

    cmd = cli.parse(Lists.newArrayList("a", "b"));
    CopyCli cp = new CopyCli();
    CLIConfigurator.inject(cmd, cp);
    System.out.println(cp);
  }


  /**
   * 注意option flag=true的代表取值布尔类型，true/false/0/1，所以声明时需要声明成boolean.
   * 此处用的注解在vertx中还用在了command上，如：{@link StartCommand}
   */
  @Name("copy")
  @Summary("A copy tool")
  @Description("Copy file or directory")
  @ToString
  static class CopyCli {

    private boolean recursive;
    private boolean help;
    private File source;
    private File target;

    public boolean getRecursive() {
      return recursive;
    }

    @Option(longName = "recursive", shortName = "R", flag = true)
    @Description("support for dir")
    public void setRecursive(boolean recursive) {
      this.recursive = recursive;
    }

    public boolean getHelp() {
      return help;
    }

    @Option(longName = "help", shortName = "h", flag = true, help = true)
    @Description("help for usage")
    public void setHelp(boolean help) {
      this.help = help;
    }

    public File getSource() {
      return source;
    }

    @Argument(argName = "source", index = 0)
    @Description("copy from ")
    public void setSource(File source) {
      this.source = source;
    }

    public File getTarget() {
      return target;
    }

    @Argument(argName = "target", index = 1)
    @Description("copy to ")
    public void setTarget(File target) {
      this.target = target;
    }
  }
}
